#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
简化版示例Python脚本，仅保留必要内容
"""

import sys

def concatenate_texts():
    """
    拼接输入框1和输入框2的文本内容，设置到输入框3中
    """
    try:
        # 获取环境管理器实例
        env_manager = sys.modules['__main__'].env_manager
        
        # 获取输入框对象引用
        input1 = env_manager.get_object_reference("input1")
        input2 = env_manager.get_object_reference("input2")
        input3 = env_manager.get_object_reference("input3")
        
        # 从输入框1和输入框2读取文本内容
        text1 = input1.property("text")
        text2 = input2.property("text")
        
        # 拼接文本
        concatenated_text = text1 + text2
        
        # 设置到输入框3中
        input3.setProperty("text", concatenated_text)
        
        # 确保返回的字符串能正确显示
        result = f"成功拼接: '{text1}' + '{text2}' = '{concatenated_text}'"
        # 直接返回UTF-8编码的字符串，让C++端正确处理
        return result
        
    except Exception as e:
        return f"错误: {str(e)}"

def on_button_clicked():
    """
    按钮点击时调用的函数
    """
    try:
        # 获取环境管理器实例
        env_manager = sys.modules['__main__'].env_manager
        
        # 获取按钮对象引用
        button = env_manager.get_object_reference("executeButton")
        
        # 执行文本拼接
        result = concatenate_texts()
        
        return result
        
    except Exception as e:
        return f"按钮点击处理错误: {str(e)}"

def on_property_changed():
    """
    当属性改变时调用的函数
    """
    try:
        # 获取环境管理器实例
        env_manager = sys.modules['__main__'].env_manager
        
        # 获取对象引用
        widget1 = env_manager.get_object_reference("widget1")
        widget2 = env_manager.get_object_reference("widget2")
        
        # 从widget1读取属性
        title1 = widget1.property("windowTitle")
        
        # 修改widget2的属性
        widget2.setProperty("windowTitle", f"Modified: {title1}")
        
        result = f"Successfully synchronized: {title1}"
        # 直接返回UTF-8编码的字符串，让C++端正确处理
        return result
        
    except Exception as e:
        return f"Error in on_property_changed: {str(e)}"

def custom_processing(input_data):
    """
    自定义处理函数
    """
    try:
        # 获取环境管理器实例
        env_manager = sys.modules['__main__'].env_manager
        
        # 获取对象引用
        widget1 = env_manager.get_object_reference("widget1")
        
        # 处理输入数据
        processed_data = input_data.upper()
        
        # 设置自定义属性
        widget1.setProperty("processed_data", processed_data)
        widget1.setProperty("data_length", len(processed_data))
        
        result = processed_data
        # 直接返回UTF-8编码的字符串，让C++端正确处理
        return result
        
    except Exception as e:
        return f"Error in custom_processing: {str(e)}"

def demo_cross_object_interaction():
    """
    演示跨对象交互
    """
    try:
        # 获取环境管理器实例
        env_manager = sys.modules['__main__'].env_manager
        
        # 获取所有注册的对象
        object_ids = env_manager.get_all_object_ids()
        
        result = "Registered objects: "
        for obj_id in object_ids:
            obj = env_manager.get_object_reference(obj_id)
            name = obj.objectName() if obj.objectName() else "Unnamed"
            result += f"{obj_id}({name}) "
        
        # 直接返回UTF-8编码的字符串，让C++端正确处理
        return result.strip()
        
    except Exception as e:
        return f"Error in demo_cross_object_interaction: {str(e)}"